<template>
  <div class="rule-container">
    <div class="create-container">
      <el-button type="primary" @click="add">增加停车计费规则</el-button>
      <el-button @click="exportexcel()">导出Excel</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="ruleList" style="width: 100%">
        <el-table-column type="index" label="序号" />
        <el-table-column label="计费规则编号" prop="ruleNumber" />
        <el-table-column label="计费规则名称" prop="ruleName" />
        <el-table-column label="免费时长(分钟)" prop="freeDuration" />
        <el-table-column label="收费上线(元)" prop="chargeCeiling" />
        <el-table-column label="计费方式">
          <template #default="scope">
            <span v-if="scope.row.chargeType == 'duration'">时长收费</span>
            <span v-if="scope.row.chargeType == 'turn'">按次收费</span>
            <span v-if="scope.row.chargeType == 'partition'">分段收费</span>
          </template>
        </el-table-column>
        <el-table-column label="计费规则" prop="ruleNameView" />
        <el-table-column label="操作" fixed="right" width="120">
          <template #default="scope">
            <el-button size="mini" type="text" @click="edit(scope.row.id)"
              >编辑</el-button
            >
            <el-button size="mini" type="text" @click="shanchu(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        layout="total, prev, pager, next"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="form.page"
        :page-size="form.pageSize"
      />
    </div>
    <!-- 弹框 -->
    <el-dialog :visible="dialogVisible" width="680px" :title="title">
      <!-- 表单接口 -->
      <div class="form-container">
        <el-form
          ref="addForm"
          :model="addForm"
          :rules="addFormRules"
          label-position="top"
        >
          <el-form-item label="计费规则编号" prop="ruleNumber">
            <el-input v-model="addForm.ruleNumber" />
          </el-form-item>
          <el-form-item label="计费规则名称" prop="ruleName">
            <el-input v-model="addForm.ruleName" />
          </el-form-item>
          <el-form-item label="计费方式" prop="chargeType">
            <el-radio-group v-model="addForm.chargeType" size="small">
              <el-radio label="duration" border>时长收费</el-radio>
              <el-radio label="turn" border>按次收费</el-radio>
              <el-radio label="partition" border>分段收费</el-radio>
            </el-radio-group>
          </el-form-item>
          <div class="flex-container">
            <el-form-item label="免费时长">
              <el-input v-model="addForm.freeDuration" />
            </el-form-item>
            <el-form-item label="收费上限">
              <el-input v-model="addForm.chargeCeiling" />
            </el-form-item>
          </div>
          <el-form-item label="计费规则">
            <!-- 按时长收费区域 -->
            <div class="duration" v-if="addForm.chargeType == 'duration'">
              每
              <el-input
                style="width: 200px"
                v-model="addForm.durationTime"
                class="input-box"
              />
              小时
              <el-input
                style="width: 200px"
                v-model="addForm.durationPrice"
                class="input-box"
              />
              元
            </div>
            <!-- 按次收费区域 -->
            <div class="turn" v-if="addForm.chargeType == 'turn'">
              每次 <el-input v-model="addForm.turnPrice" class="input-box" /> 元
            </div>
            <!-- 按分段收费区域 -->
            <div class="partition" v-if="addForm.chargeType == 'partition'">
              <div class="item">
                <el-input
                  style="width: 50px"
                  v-model="addForm.partitionFrameTime"
                  class="input-box"
                />小时内,每小时收费<el-input
                  style="width: 50px"
                  v-model="addForm.partitionFramePrice"
                  class="input-box"
                />
                元
              </div>
              <div class="item">
                每增加<el-input
                  style="width: 50px"
                  v-model="addForm.partitionIncreaseTime"
                  class="input-box"
                />小时,增加<el-input
                  style="width: 50px"
                  v-model="addForm.partitionIncreasePrice"
                  class="input-box"
                />
                元
              </div>
            </div>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="submit">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<style scoped>
.partition {
  display: flex;
  justify-content: space-between;
}
.duration {
  display: flex;
  justify-content: space-between;
}
</style>
<script>
import {
  addjfgzgl,
  editjfgzgl,
  jfgzgl,
  jfgzglshanchu,
  jfgzglxq,
} from "@/api/user";
import { utils, writeFileXLSX } from "xlsx";

export default {
  name: "Building",
  data() {
    return {
      title: "新增规则",
      ruleList: [],
      form: {
        page: 1,
        pageSize: 10,
      },

      addForm: {
        chargeType: "duration",
      },
      addFormRules: {
        ruleNumber: [
          { required: true, message: "请输入计费规则编号", trigger: "blur" },
        ],
        ruleName: [
          { required: true, message: "请输入计费规则名称", trigger: "blur" },
        ],
        chargeType: [
          { required: true, message: "请选择计费方式", trigger: "change" },
        ],
      },
      total: 0,
      dialogVisible: false,
    };
  },
  mounted() {
    this.getdata();
  },
  methods: {
    async exportexcel() {
      //选择要导出的数据
      const res = await jfgzgl(this.form);
      //表头英文字段
      const headekey = [
        "ruleNumber",
        "ruleName",
        "freeDuration",
        "chargeCeiling",
        "chargeType",
        "ruleNameView",
      ];
      //表头中文字段
      const headekeychinese = [
        "计费规则编号",
        "计费规则名称",
        "免费时长(分钟)",
        "收费上线(元)",
        "计费方式",
        "计费规则",
      ];
      //以excel的形式调整表格数据
      const sheetdata = res.data.rows.map((item) => {
        const obj = {};
        headekey.forEach((el) => {
          obj[el] = item[el];
        });
        return obj;
      });
      //创建一个工作表
      const worksheet = utils.json_to_sheet(sheetdata);
      //创建一个新的工作薄
      const workbool = utils.book_new();
      //把工作表添加到工作薄
      utils.book_append_sheet(workbool, worksheet, "Data");
      utils.sheet_add_aoa(worksheet, [headekeychinese], { origin: "A1" });
      writeFileXLSX(workbool, "SheetJSVueAoO.xlsx");
    },
    shanchu(id) {
      this.$confirm("是否确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await jfgzglshanchu(id);
          if (res.code == 10000) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getdata();
          } else {
            this.$message({
              type: "info",
              message: res.msg,
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async edit(id) {
      const res = await jfgzglxq(id);
      console.log(res);
      if (res.code == 10000) {
        this.title = "编辑规则";
        this.dialogVisible = true;
        this.addForm = res.data;
      }
    },
    add() {
      this.dialogVisible = true;
      this.title = "新增规则";
      this.addForm = {
        chargeType: "duration",
      };
    },
    submit() {
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          if (this.addForm.chargeType == "duration") {
            this.addForm.durationType = "hour";
          }
          if (this.title == "新增规则") {
            const res = await addjfgzgl(this.addForm);
            console.log(res);
            if (res.code == 10000) {
              this.$message.success("成功");
              this.dialogVisible = false;
              this.getdata();
            }
          } else {
            const res = await editjfgzgl(this.addForm);
            console.log(res);
            if (res.code == 10000) {
              this.$message.success("成功");
              this.dialogVisible = false;
              this.getdata();
            }
          }
        }
      });
    },
    async getdata() {
      const res = await jfgzgl(this.form);
      console.log(res, "res");
      this.ruleList = res.data.rows;
      this.total = res.data.total;
    },
    handleSizeChange(e) {
      this.form.pageSize = e;
      this.getdata();
    },
    handleCurrentChange(e) {
      this.form.page = e;
      this.getdata();
    },
  },
};
</script>

<style lang="scss" scoped>
.rule-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, 0.9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container {
  margin: 10px 0px;
}
.page-container {
  padding: 4px 0px;
  text-align: right;
}
.form-container {
  padding: 0px 80px;
}
</style>